import { combineReducers } from 'redux';
import { configureStore } from '@reduxjs/toolkit';
import { persistReducer, persistStore } from 'redux-persist'; // 持久化
import storage from 'redux-persist/lib/storage';
import storageSession from 'redux-persist/lib/storage/session';
// 导入子模块
import counterReducer from './modules/counterStore';
import navReducer from './modules/navStore';
// 持久化
const rootReducer = combineReducers({
  counter: counterReducer,
  nav: navReducer,
});

// 持久化配置
const persistConfig = {
  key: 'root',
  storage: storageSession,
  whitelist: ['nav'], // 指定需要持久化的 reducer 键名
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

// 创建根store组合子模块
const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false, // 避免由于使用 Immutable.js 而引发的序列化检查错误
    }),
});
// 创建持久化存储
export const persistor = persistStore(store);
export default store;
